package com.furkid.service.service.impl;

import com.furkid.service.common.exception.BizException;
import com.furkid.service.domain.user.User;
import com.furkid.service.mapper.UserMapper;
import com.furkid.service.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class IUserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public User userAccountLogin(String account, String password) {
        // 1) 入参校验（业务异常）
        if (account == null || account.isBlank() || password == null || password.isBlank()) {
            throw new BizException(400, "用户名或密码不能为空");
        }

        // 2) 查询用户（查不到 = 业务异常）
        User user = userMapper.searchUserImpl(account);
        if (user == null) {
            // 不要泄露是账号错还是密码错
            throw new BizException(400, "用户不存在");
        }

        // 3) 校验状态（业务异常）
        if (user.getStatus() == 0) {
            throw new BizException(403, "账号已注销");
        }

        // 4) 校验密码（建议用 PasswordEncoder / BCrypt；这里仅示例）
        boolean ok = password.equals(user.getPassword());
        if (!ok) {
            throw new BizException(400, "密码错误");
        }

        user.setPassword(null);

        // 5) 成功：不要在日志里打印敏感信息
        log.info("user login success, uid={}", user.getId());
        return user;
    }
}
